<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<!-- <el-form-item>
					<el-input v-model="filters.name" placeholder="设备IP"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary">查询</el-button>
				</el-form-item> -->
				<el-date-picker
			      v-model="exprotDate"
			      type="daterange"
      			  range-separator="至"
			      placeholder="请选择开始和结束时间">
			    </el-date-picker>
				<el-form-item v-show="my_permi.indexOf('snapAbnormal.export') > -1 || my_permi.indexOf('admin') > -1">
					<el-button type="primary" @click="exportT">导出</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="tableList" highlight-current-row v-loading="listLoading" style="width: 100%;" :height="clientHeight" >
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="ic_id" label="IC卡卡号">
			</el-table-column>
			<el-table-column prop="vehicle_number" label="车牌号">
			</el-table-column>
			<el-table-column prop="capture_vehicle_number" label="车牌识别">
			</el-table-column>
			<el-table-column prop="capture_filename" label="抓拍图片">
				<template slot-scope="scope">
					<img class="scope-img" @click="isZoom(scope.row.capture_filename)" :src="scope.row.capture_filename">
				</template>
			</el-table-column>
			<el-table-column prop="abnormal_information" label="异常信息">
			</el-table-column>
			<el-table-column prop="handling_situation_desc" label="处理情况">
			</el-table-column>
			<el-table-column prop="system_date" label="系统时间" width="110">
			</el-table-column>
			<el-table-column prop="remark" label="备注">
			</el-table-column>
			<el-table-column label="处理" width="210" v-show="my_permi.indexOf('snapAbnormal.handlingSituation.index') > -1 || my_permi.indexOf('admin') > -1">
				<template slot-scope="scope">
					<el-button type="warning" size="small" @click="handleEdit(scope.$index, scope.row)">处理情况</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination
				layout="prev, pager, next"
				@current-change="handleCurrentChange"
				:total="total"
				style="float:right;">
			</el-pagination>
		</el-col>

		<!-- 图片放大 -->
		<el-dialog title="展示图片" v-model="is_zoom_img" class="zoom-img-dialog" :close-on-click-modal="false">
			<img :src="zoom_img">
		</el-dialog>

		<!--编辑界面-->
		<el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" ref="editForm">
				<el-form-item label="处理情况">
					<el-select v-model="editFormSelect.handling_situation" placeholder="请选择" @change="changehanler">
					    <el-option
					      v-for="item in options"
					      :key="item.value"
					      :label="item.label"
					      :value="item.value">
					    </el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import { BASEURL, snapAbnormal, getHandlingSituation, editSnapAbnormal } from '@/api/api';
    import { mapState } from 'vuex'
	export default {
		data() {
			return {
				filters: {
					name: ''
				},
				tableList: [],
				total: 0,
				page: 1,
				listLoading: false,
				is_zoom_img: false,
				zoom_img: '',
		        options: [],
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				//编辑界面数据
				editForm: {
					handling_situation: '',
				},
				editFormSelect: {},
				exprotDate: '',
			}
		},
        computed: {

            ...mapState(
                {
                  'my_permi': state => state.my_permi,
                  'clientHeight': state => state.clientHeight
                }
            )
        },
		methods: {
			router(path) {
				this.$router.push(path)
			},
			handleCurrentChange(val) {
				this.page = val;
				this.snap_abnormal();
			},
			//获取设备警报信息
			snap_abnormal() {
				this.listLoading = true;
				snapAbnormal(this.page).then((res) => {
					if(res.data.status.errCode == '10002'){
						sessionStorage.removeItem('token');
						this.$message({
							message: res.data.status.message,
							type: 'warning'
				        });
				        setTimeout(() =>{
				        	this.router({path: '/login'})
				        }, 1500);
						return;
					}
					this.total = res.data.data.total
					this.tableList = res.data.data.data
					this.listLoading = false
				});
			},
			isZoom(imgSrc) {
				this.zoom_img = imgSrc
				this.is_zoom_img = !this.is_zoom_img
			},
			get_handling_situation() {
				getHandlingSituation().then((res) => {
					this.options = [];
					for(var i in res.data.data){
						var arr = {
							label: res.data.data[i].value,
							value: res.data.data[i].key,
						}
						this.options.push(arr)
					}
				})
			},
			changehanler(value) {
				this.editForm.handling_situation = value
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.ic_id = row.id;
				this.editFormSelect = {
					handling_situation: row.handling_situation_desc,
				}
				this.editForm = {
					handling_situation: row.handling_situation,
				};
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							let para = Object.assign({}, this.editForm);
							editSnapAbnormal(this.ic_id, 'PUT', para).then((res) => {
								if(res.data.status.errCode != '0'){
									this.$message({
										message: res.data.status.message,
										type: 'error'
									});
								}else{
									this.$message({
										message: res.data.status.message,
										type: 'success'
									});
								}
								this.editLoading = false;
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.snap_abnormal();
							}).catch((err) => {
								console.dir(err)
							});
						});
					}
				});
			},
			exportT() {
				let startD = "";
				let overD = "";
				let firstDate = "";
				let secendDate = "";
				if(this.exprotDate){
					firstDate = new Date(this.exprotDate[0])
					secendDate = new Date(this.exprotDate[1])
				}else{
					firstDate = new Date();
					secendDate = new Date();
				}
				startD = firstDate.getFullYear() + '-' + (firstDate.getMonth()+1) + '-' + firstDate.getDate()
				overD = secendDate.getFullYear() + '-' + (secendDate.getMonth()+1) + '-' + secendDate.getDate()
				location.href=`${BASEURL}/SnapAbnormal/Export?token=${sessionStorage.token}&start_time=${startD}&end_time=${overD + ' 23:59:59'}&pageSize=${sessionStorage.getItem('pagesize')}`;
			},
		},
		mounted() {
			this.snap_abnormal();
			this.get_handling_situation()
		}
	}

</script>

<style>
.el-select{
	display: block;
}
.el-select__tags{
	max-width: auto;
}
</style>